package online.hxstart.code.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/** @author huxing */
public class LengthOfLongestSubstring {
  public int lengthOfLongestSubstring(String s) {
    char[] chars = s.toCharArray();
    Map<Character, Integer> map = new HashMap<>(chars.length);
    int a, b = 0;
    Set<Character> set = new HashSet<>();
    if (chars.length > 1) {
      for (int i = 0; i < chars.length - 1; i++) {
        a = 1;
        set.clear();
        for (int j = i + 1; j < chars.length; j++) {
          if (set.add(s.charAt(j))) {
            if (s.charAt(i) == s.charAt(j)) {
              if (a > b) {
                b = a;
              }
              break;
            } else {
              a++;
            }
          } else {
            break;
          }
        }
        if (a > b) {
          b = a;
        }
      }
    } else {
      return chars.length;
    }
    return b;
  }

  public static void main(String[] args) {
    LengthOfLongestSubstring a = new LengthOfLongestSubstring();
    System.out.println(a.lengthOfLongestSubstring("dvdf"));
  }
}
